﻿using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace HVACWebService.Common
{
    public class WebAccess
    {
        public static string Call(string url, object postData)
        {
            try
            {
                var isPost = postData != null;
                var webReq = (HttpWebRequest)WebRequest.Create(url);
                webReq.Method = isPost ? "POST" : "GET";
                webReq.ContentType = "application/json; charset=UTF-8";

                if (isPost)
                {
                    var date = JsonSerializer.Instance.Serialize(postData);
                    var bytes = Encoding.UTF8.GetBytes(date);
                    webReq.ContentLength = bytes.Length;

                    using (var writeStream = webReq.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
                using (var response = (HttpWebResponse)webReq.GetResponse())
                {
                    var responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            var result = readStream.ReadToEnd();

                            return result;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                
            }
            return string.Empty;
        }

        public static string Call(string url, string postData)
        {
            try
            {
                var isPost = !string.IsNullOrEmpty(postData);
                var webReq = (HttpWebRequest)WebRequest.Create(url);
                webReq.Method = isPost ? "POST" : "GET";
                webReq.ContentType = "application/json; charset=UTF-8";

                if (isPost)
                {
                    var bytes = Encoding.UTF8.GetBytes(postData);
                    webReq.ContentLength = bytes.Length;

                    using (var writeStream = webReq.GetRequestStream())
                    {
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
                using (var response = (HttpWebResponse)webReq.GetResponse())
                {
                    var responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            var result = readStream.ReadToEnd();

                            return result;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
             
            }
            return string.Empty;
        }

        /// <summary>
        /// Calls the cert.
        /// </summary>
        /// <param name="posturl">The posturl.</param>
        /// <param name="postData">The post data.</param>
        /// <param name="certPath">CerPath证书路径 The cert path.</param>
        /// <param name="password">证书密码 The password.</param>
        /// <returns></returns>
        public static string CallCert(string posturl, string postData, string certPath, string password)
        {
            var encoding = Encoding.UTF8;
            var data = encoding.GetBytes(postData);
            try
            {
                var cert = new X509Certificate2(certPath, password, X509KeyStorageFlags.MachineKeySet);
                var request = WebRequest.Create(posturl) as HttpWebRequest;
                var cookieContainer = new CookieContainer();
                if (request == null) return string.Empty;
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.ContentLength = data.Length;
                request.ClientCertificates.Add(cert);
                var outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                var response = request.GetResponse() as HttpWebResponse;
                if (response == null) return string.Empty;
                var instream = response.GetResponseStream();
                if (instream == null) return string.Empty;
                var sr = new StreamReader(instream, encoding);
                var content = sr.ReadToEnd();
                return content; 
            }
            catch (Exception ex)
            {
                var err = ex.Message;
                return string.Empty;
            }
        }
    }
}
